//
// Created by 吴喆 on 2020/3/24.
//

#include "Bullet.h"

Bullet::Bullet(const Scene *scene, Playable *parent, const glm::vec2 startPosition) : Playable(scene), parent(parent) {
    objectType = OBJECT_TYPE::BULLET;
    GameObject::initPosition = startPosition;
    Playable::direction = parent->direction;
//    rotate = acos(glm::dot(glm::normalize(direction), glm::vec2(1, 0)));
    speed = speed * 2;
}

void Bullet::init() {
    initGameObject();
    initBox();
    initStateMachine();
    initParticleGenerator();
}

void Bullet::initBox() {
    b2BodyDef bodyDef;
    bodyDef.userData = this;
    bodyDef.type = b2_dynamicBody;
    body.currentPos = body.startPos = glm::vec2(-GameObject::initPosition.x, GameObject::initPosition.y + GameObject::size.y / 2);

    bodyDef.position.Set(-GameObject::initPosition.x - GameObject::size.x / 2,
                         body.startPos.y);
    bodyDef.bullet = true;
    body.instance = scene->world->createBody(&bodyDef);
    body.instance->SetFixedRotation(true);

    body.dynamicBox.SetAsBox(0.1, 0.1);
    // Define the dynamic body fixture.
    body.fixtureDef.shape = &body.dynamicBox;
    body.instance->CreateFixture(&body.fixtureDef);
    body.instance->SetUserData(this);
    body.instance->GetFixtureList()->SetSensor(true);

}

void Bullet::update() {
    Playable::update();
}

Bullet::~Bullet() {
        delete particleGenerator;
}




